<?php
function is_negative_cash_allowed($ldname)
{
	$query=mysql_query("select * from bs_ledgers where ldname='{$ldname}'");
	if(!$query)
			echo("Error in Query Execution".mysql_error());
	else
	{
		$a=mysql_fetch_array($query);
		$groupid=$a['under'];
	}
	
	$result8=mysql_query("select * from bs_groups where id='{$groupid}'");
	if(!$result8)
		echo("Error in Query Execution");
	$go=1;
	while($go==1)
	{
		$arr8=mysql_fetch_array($result8);
//		echo $arr['gpname']." ".$arr['under'];
		if($arr8['gpname']=="Cash-in-hand" )//or $arr8['gpname']=="Bank Account") for overdraft a/c
		{
				return 0;
//				
				$go=0;
				break;
		}
		else if($arr8['under']==0)
		{
//				
				return 1;
				$go=0;
				break;
		}
		$query8="select * from bs_groups where id={$arr8['under']}";
		$result8=mysql_query($query8);
	}
			
				
}

function is_sc_sd($ldname)
{
	$query=mysql_query("select * from bs_ledgers where ldname='{$ldname}'");
	if(!$query)
			echo("Error in Query Execution".mysql_error());
	else
	{
		$a=mysql_fetch_array($query);
		$groupid=$a['under'];
	}
	
	$result8=mysql_query("select * from bs_groups where id='{$groupid}'");
	if(!$result8)
		echo("Error in Query Execution");
	$go=1;
	while($go==1)
	{
		$arr8=mysql_fetch_array($result8);
//		echo $arr['gpname']." ".$arr['under'];
		if($arr8['gpname']=="Sundry Debtors" or $arr8['gpname']=="Sundry Creditors")
		{
				return 1;
//				
				$go=0;
				break;
		}
		else if($arr8['under']==0)
		{
//				
				return 0;
				$go=0;
				break;
		}
		$query8="select * from bs_groups where id={$arr8['under']}";
		$result8=mysql_query($query8);
	}
			
				
}


function is_cash_bank($ldname)
{
	$query=mysql_query("select * from bs_ledgers where ldname='{$ldname}'");
	if(!$query)
			echo("Error in Query Execution".mysql_error());
	else
	{
		$a=mysql_fetch_array($query);
		$groupid=$a['under'];
	}
	
	$result8=mysql_query("select * from bs_groups where id='{$groupid}'");
	if(!$result8)
		echo("Error in Query Execution");
	$go=1;
	while($go==1)
	{
		$arr8=mysql_fetch_array($result8);
//		echo $arr['gpname']." ".$arr['under'];
		if($arr8['gpname']=="Cash-in-hand" or $arr8['gpname']=="Bank Account")
		{
				return 1;
				
				$go=0;
				break;
		}
		else if($arr8['under']==0)
		{
				
				return 0;
				$go=0;
				break;
		}
		$query8="select * from bs_groups where id={$arr8['under']}";
		$result8=mysql_query($query8);
	}
			
				
}


function extract_ledger_with_balance($group_id)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
			if($arr['ldname']!=$escape_ledger_in_extract)
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_with_balance($arr['id']);
	}
	
//		return $count;
} 										
function extract_ledger_with_balance_wc($group_id,$ldid)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
			if($arr['id']!=$ldid)
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
			else
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			
			}
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_with_balance_wc($arr['id'],$ldid);
	}
	
//		return $count;
} 										
function extract_ledger_except_cash_bank_with_balance($group_id)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
			if(!is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_except_cash_bank_with_balance($arr['id']);
	}
	
//		return $count;
} 										

function extract_ledger_except_cash_bank_with_balance_wc($group_id,$ldid)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		if($arr['id']!=$ldid)
		{	
			if(!is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
		}
		else
		{	
			if(!is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
		}		
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_except_cash_bank_with_balance_wc($arr['id'],$ldid);
	}
	
//		return $count;
} 										

function extract_ledger_cash_bank_with_balance_wc($group_id,$ldid)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
	
		if($arr['id']!=$ldid)
		{
			if(is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
		}
		else
		{
			if(is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
		
		
		}
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_cash_bank_with_balance_wc($arr['id'],$ldid);
	}
	
//		return $count;
} 										

function extract_ledger_cash_bank_with_balance($group_id)
{
	global $count,$escape_ledger_in_extract;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
	
			if(is_cash_bank($arr['ldname']))
			{
				$ledgername="bs_ledger_".ereg_replace(" ","_",$arr['ldname']);
				$result1=mysql_query("select (SUM(debit)-SUM(credit)) as balance from {$ledgername}");
				if(!$result1)
						echo "Internal Select Query Error".mysql_error();
				$arr1=mysql_fetch_array($result1);
				$arr1['balance']=(int)$arr1['balance'];
				if($arr1['balance']>=0)
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Dr</option>");
				else
				{
						$arr1['balance']=abs($arr1['balance']);
						echo ("<option value='{$arr['ldname']}'>{$arr['ldname']} : {$arr1['balance']} Rs : Cr</option>");
				}
				$count++;
			}
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_cash_bank_with_balance($arr['id']);
	}
	
//		return $count;
} 
function extract_ledger($group_id)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		echo ("<option value='{$arr['ldname']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger($arr['id']);
	}
	
//		return $count;
} 										


function extract_ledger_with_choice($group_id,$ldid)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		if($arr['id']==$ldid)
			echo ("<option value='{$arr['id']}' selected='selected'>{$arr['ldname']}</option>");
		else
			echo ("<option value='{$arr['id']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_with_choice($arr['id'],$ldid);
	}
	
//		return $count;
} 										

function extract_ledger_ids_under($group_id)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		echo ("<option value='{$arr['id']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_ids_under($arr['id']);
	}
	
//		return $count;
} 										


function extract_ledger_except_cash_bank($group_id)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		if(!is_cash_bank($arr['ldname']))
			echo ("<option value='{$arr['ldname']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_except_cash_bank($arr['id']);
	}
	
//		return $count;
} 

function extract_ledger_ids_except_cash_bank($group_id)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		if(!is_cash_bank($arr['ldname']))
			echo ("<option value='{$arr['id']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_ids_except_cash_bank($arr['id']);
	}
	
//		return $count;
} 

function extract_ledger_ids_cash_bank($group_id)
{
	global $count;
	//ADD ALL LEDGERS UNDER $ID;
	$result=mysql_query("select * from bs_ledgers where under={$group_id} order by ldname");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		if(is_cash_bank($arr['ldname']))
			echo ("<option value='{$arr['id']}'>{$arr['ldname']}</option>");
		$count++;
	}
	//LIST=SELECT ALL GROUP UNDER $ID
	$result=mysql_query("select * from bs_groups where under={$group_id}");
	if(!$result)
			echo("Error in Query Execution");
	while($arr=mysql_fetch_array($result))
	{
		extract_ledger_ids_cash_bank($arr['id']);
	}
	
//		return $count;
} 

function calculate_trial_balance_of_ledger($ldname,$under)
{
		global $debit,$credit,$fromdate,$todate,$pandl;
		$ldname="bs_ledger_".ereg_replace(" ","_",$ldname);
		

		//WHAT IS THE NATURE OF LEDGER																		
		$under_group=$under;
		$traced=0;
		while($traced==0)
		{
				$result=mysql_query("select * from bs_groups where id='{$under_group}'");
				if(!$result)
						echo "error";
				else
				{
						$arr=mysql_fetch_array($result);
						if($arr['under']=='00')
						{
								$traced=1;
								break;
						}
						else
						{
								$under_group=$arr['under'];
						}
		
				}
		}
							
		$nature=$arr['nature'];
					
		//WHAT IS THE NATURE OF LEDGER : END
		
		if($nature=="EXP" or $nature=="INC")
		{
				$result2=mysql_query("select SUM(debit)-SUM(credit) as balance from {$ldname} where date>='{$fromdate}' and date<='{$todate}' ");
		}
		else 	
		{
				$result2=mysql_query("select SUM(debit)-SUM(credit) as balance from {$ldname} where date<='{$todate}' ");
		}

		if(!$result2)
				echo "Error : ".mysql_error();
									
		$arr2=mysql_fetch_array($result2);
						
		$balance=(int)$arr2['balance'];
		
		if($balance<0)
				$credit=abs($balance);
		else
				$debit=$balance;

}


function calculate_trial_balance_of_group($group_id)
{
	global $debit,$credit,$fromdate,$todate,$pandl,$diffopb;

	// SELECT ALL GROUP UNDER THIS GROUP
	//		FOR EACH GROUP RECURSIVE CALL  
	
	// SELECT ALL LEDGER UNDER THIS GROUP
	// FOR EACH LEDGER INCREMENT DEBIT OR CREDIT VARIABLE 
	// RETURN;	
	$result=mysql_query("select * from bs_groups where under='{$group_id}'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{
					//GO TILL THE LAST POINT
					calculate_trial_balance_of_group($arr['id']);
		
			}
			
			//FETCH ALL LEDGERS UNDER THIS GROUP
			$result1=mysql_query("select * from bs_ledgers where under='{$group_id}'");
			if(!$result1)
					echo "Error : ".mysql_error();
			else
			{
					while($arr1=mysql_fetch_array($result1))
					{
							//WHAT IS THE NATURE OF LEDGER																		
							$under_group=$arr1['under'];
							$traced=0;
							while($traced==0)
							{
									$result2=mysql_query("select * from bs_groups where id='{$under_group}'");
									if(!$result2)
											echo "error";
									else
									{
											$arr2=mysql_fetch_array($result2);
											if($arr2['under']=='00')
											{
													$traced=1;
													break;
											}
											else
											{
												$under_group=$arr2['under'];
											}
						
									}
							}
							
							$nature=$arr2['nature'];
							
							//WHAT IS THE NATURE OF LEDGER : END

							$ldname="bs_ledger_".ereg_replace(" ","_",$arr1['ldname']);
							
							//FIRST CALCULATE DIFFOPB
							$result28=mysql_query("select SUM(debit)-SUM(credit) as diffopb from {$ldname} where voucher_type='OPB' ");
							if(!$result28)
									echo "error";
							else
							{
									$arr28=mysql_fetch_array($result28);
									$diffopb+=(int)$arr28['diffopb'];
							}
							
							
							
							//END
					
							if($nature=="EXP" or $nature=="INC")
							{
									//WHEN GROUP ID =0 IN DISPLAY_TRIAL_BALANCE FUNCTION  (THE TRIGGER FUNCTION)  ALL LEDGERS WILL BE TRACED AND I WILL CALCULATE PANDL AND DISPLAY FOR INC AND EXP A/C!
									if($nature=="INC")
									{
											//SALES IS CREDITED SO..
											$result28=mysql_query("select SUM(credit)-SUM(debit) as balance from {$ldname} where date<'{$fromdate}' ");
											if(!$result28)
													echo "error";
											else
											{
													$arr28=mysql_fetch_array($result28);
													$pandl=$pandl+(int)$arr28['balance']; //BECUASE I AM IN PROFIT :)
											}
									}	
									else if($nature=="EXP")
									{
											//PURCHASE AND EXPENSES ARE DEBITED SO...
											$result28=mysql_query("select SUM(debit)-SUM(credit) as balance from {$ldname} where date<'{$fromdate}' ");
											if(!$result28)
													echo "error";
											else
											{
													$arr28=mysql_fetch_array($result28);
													$pandl=$pandl-(int)$arr28['balance']; //BECUASE I AM IN LOSS :(
											}
									}
									
									$result2=mysql_query("select SUM(debit)-SUM(credit) as balance from {$ldname} where date>='{$fromdate}' and date<='{$todate}' ");
					
							}
							else 	
									$result2=mysql_query("select SUM(debit)-SUM(credit) as balance from {$ldname} where date<='{$todate}' ");
									
							if(!$result2)
									echo "Error : ".mysql_error();
									
							$arr2=mysql_fetch_array($result2);
							
							$balance=(int)$arr2['balance'];
							if($balance<0)
									$credit=$credit+abs($balance);
							else
									$debit=$debit+$balance;

									
		
					}
			}

	
	}

}



function display_trial_balance_of($group_id)
{
	global $sum_debit,$sum_credit,$pandl,$diffopb;
	$sum_debit=0;$sum_credit=0;

	if($group_id=='00')
			echo "<table cellpadding='3' class='trial_list' border='1'>";
	else
			echo "<table cellpadding='3' class='trial_list' border='1'>"; //ambigu ...but may be changed later :)
		
	echo "<tr><th>Particulars</th><th>Debit</th><th>Credit</th></tr>";
	
	//FIRST EXTRACT ALL GROUPS UNDER THIS GROUP
	$result=mysql_query("select * from bs_groups where under='{$group_id}'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{	
					global $debit,$credit;
					$debit=0;$credit=0;
					calculate_trial_balance_of_group($arr['id']);
					

					if($debit!=0 or $credit!=0)
					{
							echo "<tr><td><a href='trial_balance.php?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&group_id={$arr['id']}'> {$arr['gpname']}</a></td> ";
							echo " <td>{$debit}</td> <td>{$credit}</td></tr>";
							$sum_debit+=$debit;
							$sum_credit+=$credit;
					}
	

				
		
			}
	
	}
	
	//SECOND EXTRACT ALL LEDGER THIS GROUP
	$result=mysql_query("select * from bs_ledgers where under='{$group_id}' order by ldname");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{
					global $debit,$credit;		
					$debit=0;$credit=0;
					calculate_trial_balance_of_ledger($arr['ldname'],$arr['under']);
					
//					if($debit!=0 or $credit!=0)
	//				{
							echo "<tr><td><a href='display_ledger.php?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&ledger_id={$arr['id']}'> <em>{$arr['ldname']}</em></a></td>";
							echo "<td>{$debit}</td> <td>{$credit}</td></tr>";
							$sum_debit+=$debit;
							$sum_credit+=$credit;
							
		//			} 	NOT APPROPRIATE FOR LEDGER MY SBBJ =40000-40000 =0 !

		
			}
	
	}
	//DISPLAY PROFIT AND LOSS
	if($group_id=='00' && $pandl!=0)
	{
			//ALSO DISPLAY P & L AND DIFFERENCES AND OPENING BALANCES
			if($pandl>0)
			{
					echo"<tr><td>Profit & Loss</td><td>0</td><td>{$pandl}</td></tr>";			
					$sum_credit+=$pandl;
			}
			else
			{
					$pandl=abs($pandl);
					echo"<tr><td>Profit & Loss</td><td>{$pandl}</td><td>0</td></tr>";			
					$sum_debit+=$pandl;					
					
			}
					
			
	}
	
	//DISPLAY DIFFERENCE IN OPENING BALANCE
	if($group_id=='00' && $diffopb!=0)
	{

			if($diffopb>0)
			{
					//DO FAIR WITH CREDIT SIDE
					echo"<tr><td>Difference in Opening Balances</td><td>0</td><td>{$diffopb}</td></tr>";			
					$sum_credit+=$diffopb;
			}
			else
			{
					//DO FAIR WITH DEBIT SIDE
					$diffopb=abs($diffopb);
					echo"<tr><td>Difference in Opening Balances</td><td>{$diffopb}</td><td>0</td></tr>";			
					$sum_debit+=$diffopb;					
					
			}
					
			
	}

	echo"<tr><td><b>Total</b></td><td><b>{$sum_debit}</b></td><td><b>{$sum_credit}</b></td></tr>";
	echo "</table>";


}

//FOR BALANCE SHEET
function display_trial_balance_of_forbs($group_id)
{
	global $sum_debit,$sum_credit,$pandl,$diffopb;
	$sum_debit=0;$sum_credit=0;

	echo "<table cellpadding='3' class='main_margin' border='1'>";
		
//	echo "<tr><th>Particulars</th><th>Debit</th><th>Credit</th></tr>";
	
	//FIRST EXTRACT ALL GROUPS UNDER THIS GROUP
	$result=mysql_query("select * from bs_groups where under='{$group_id}'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{	
					global $debit,$credit;
					$debit=0;$credit=0;
					calculate_trial_balance_of_group($arr['id']);
					

					if($debit!=0 or $credit!=0)
					{
							echo "<tr><td><a href='trial_balance.php?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&group_id={$arr['id']}'> {$arr['gpname']}</a></td> ";
							echo " <td>{$debit}</td> <td>{$credit}</td></tr>";
							$sum_debit+=$debit;
							$sum_credit+=$credit;
					}
	

				
		
			}
	
	}
	
	//SECOND EXTRACT ALL LEDGER THIS GROUP
	$result=mysql_query("select * from bs_ledgers where under='{$group_id}'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{
					global $debit,$credit;		
					$debit=0;$credit=0;
					calculate_trial_balance_of_ledger($arr['ldname'],$arr['under']);
					
//					if($debit!=0 or $credit!=0)
	//				{
							echo "<tr><td><a href='display_ledger.php?fromdate={$_GET['fromdate']}&todate={$_GET['todate']}&task=display&ledger_id={$arr['id']}'> <em>{$arr['ldname']}</em></a></td>";
							echo "<td>{$debit}</td> <td>{$credit}</td></tr>";
							$sum_debit+=$debit;
							$sum_credit+=$credit;
							
		//			} 	NOT APPROPRIATE FOR LEDGER MY SBBJ =40000-40000 =0 !

		
			}
	
	}
	//DISPLAY PROFIT AND LOSS //remain of trial blnces
	if($group_id=='00' && $pandl!=0)
	{
			//ALSO DISPLAY P & L AND DIFFERENCES AND OPENING BALANCES
			if($pandl>0)
			{
					echo"<tr><td>Profit & Loss</td><td>0</td><td>{$pandl}</td></tr>";			
					$sum_credit+=$pandl;
			}
			else
			{
					$pandl=abs($pandl);
					echo"<tr><td>Profit & Loss</td><td>{$pandl}</td><td>0</td></tr>";			
					$sum_debit+=$pandl;					
					
			}
					
			
	}
	
	//DISPLAY DIFFERENCE IN OPENING BALANCE
	if($group_id=='00' && $diffopb!=0)
	{

			if($diffopb>0)
			{
					//DO FAIR WITH CREDIT SIDE
					echo"<tr><td>Difference in Opening Balances</td><td>0</td><td>{$diffopb}</td></tr>";			
					$sum_credit+=$diffopb;
			}
			else
			{
					//DO FAIR WITH DEBIT SIDE
					$diffopb=abs($diffopb);
					echo"<tr><td>Difference in Opening Balances</td><td>{$diffopb}</td><td>0</td></tr>";			
					$sum_debit+=$diffopb;					
					
			}
					
			
	}

	//echo"<tr><td><b>Total</b></td><td><b>{$sum_debit}</b></td><td><b>{$sum_credit}</b></td></tr>";
	echo "</table>";


}

function display_lib_groups()
{

//SELECT ALL GROUPS UNDER LIB NATURE
	//SELECT ALL GROUPS AND LEDGER (DEEP LEVEL 1) UNDER THOSE AND DISPLAY THERE NETT BALANCE	
	global $nett_sum_of_lib;
	$nett_sum_of_lib=0;
	$sum=0;

	
	//FIRST EXTRACT ALL GROUPS UNDER THIS GROUP
	$result=mysql_query("select * from bs_groups where nature='LIB'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			//FOR EACH SELECTED GROUP
			while($arr=mysql_fetch_array($result))
			{	
					global $debit,$credit;
					
					$debit=0;$credit=0;
					
					calculate_trial_balance_of_group($arr['id']);
					
					
					

					if($debit!=0 or $credit!=0)
					{
						$nett_sum_of_lib=($credit-$debit);
//						echo "<table  class='leading' cellpadding='3'> <tr> <td>{$arr['gpname']}</td> <td> {$nett_sum_of_lib}</td></tr></table>";
						echo "<div class='left_err'> {$arr['gpname']} </div><div class='right_err'> {$nett_sum_of_lib}</div>";
						display_trial_balance_of_forbs($arr['id']);
						$sum+=$nett_sum_of_lib;
							
					}
	
			}
	
	}
	return $sum;
	
}

function display_ast_groups()
{

//SELECT ALL GROUPS UNDER LIB NATURE
	//SELECT ALL GROUPS AND LEDGER (DEEP LEVEL 1) UNDER THOSE AND DISPLAY THERE NETT BALANCE	
	global $nett_sum_of_ast;
	$nett_sum_of_ast=0;
	$sum=0;

	
	//FIRST EXTRACT ALL GROUPS UNDER THIS GROUP
	$result=mysql_query("select * from bs_groups where nature='AST'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			//FOR EACH SELECTED GROUP
			while($arr=mysql_fetch_array($result))
			{	
					global $debit,$credit;
					
					$debit=0;$credit=0;
					
					calculate_trial_balance_of_group($arr['id']);
					

					if($debit!=0 or $credit!=0)
					{
						$nett_sum_of_ast=($debit-$credit);
						echo "<div class='left_err'> {$arr['gpname']} </div><div class='right_err'> {$nett_sum_of_ast}</div>";		
						display_trial_balance_of_forbs($arr['id']);						
						$sum+=$nett_sum_of_ast;
							
					}
	
			}
	
	}
	
	return $sum;
	
}

function calculate_p_and_l()
{
	global $pandl_current,$pandl_period;
	$pandl_current=0;

	global $pandl;//INTERMEDIATE FOR PANDL PERIOD.
	$pandl=0;
	global $debit,$credit;
	$debit=0;$credit=0;	

	// SELECT ALL GROUP UNDER NATURE EXP OR INC
	
	///  PERIODIC DATE<FROMDATE 
	$result=mysql_query("select * from bs_groups where nature='EXP' or nature='INC'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{
					//FOR ALL GROUPS ...I WANT TO FETCH THE VARIABLE $PANDL (PERIOD)
					// I ALSO KNNOW THAT THERE IS REDUNDANCY IN CALCULATE_TRIAL_BALANCE_OF_GROUP WHILE SELECTING LEDGER NATURE OF EXP AND INC!
					
					calculate_trial_balance_of_group($arr['id']);
					if($arr['nature']=="INC")
					{
							$pandl_current+=($credit-$debit);
					}
					else if($arr['nature']=="EXP")
					{
							$pandl_current-=($debit-$credit);
					}
					$debit=0;
					$credit=0;
					
		
			}
	}//END
	
	
	$pandl_period=$pandl;
			
	
	
	
	
	
}


//THIS IS THE WAY BY WHICH I CAN TRAVERSE ALL THE LEDGERS OF ALL THE GROUPS
function calculate_diffopb()
{
	global $diffopb;
	$diffopb=0;

	// SELECT ALL GROUP UNDER NATURE EXP OR INC
	
	///  PERIODIC DATE<FROMDATE 
	$result=mysql_query("select * from bs_groups where under='00'");
	if(!$result)
			echo "Error : ".mysql_error();
	else
	{
			while($arr=mysql_fetch_array($result))
			{
					calculate_trial_balance_of_group($arr['id']);
			}
	}//END
	
	
		
	
	
	
	
	
}


?>